package com.sirap.fish.fetchers;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;

import com.google.common.collect.Lists;
import com.sirap.basic.component.Extractor;
import com.sirap.basic.json.Mist;
import com.sirap.basic.json.MistUtil;
import com.sirap.basic.tool.D;
import com.sirap.basic.util.Amaps;
import com.sirap.basic.util.DateUtil;
import com.sirap.basic.util.MathUtil;
import com.sirap.basic.util.StrUtil;
import com.sirap.basic.util.XXXUtil;
import com.sirap.common.domain.Album;

public class SinaStoryImageFetchers {
	public static void main(String[] args) {
		Object va;
//		va = getAlbums(3);/
//		D.pl(countOfAlbum("https://www.fenun.cn/Item/Details/20912"));
//		va = getAlbumsByPage(2);
//		va = getAlbumsByPage(82);
		va = getAlbum("http://slide.news.sina.com.cn/z/slide_1_64237_313173.html");
		D.pjsp(va);
	}
	
	public static Map<String, Object> getAlbumsByPage(int page) {
		final int pagesize = 10;
		
		Extractor<Map<String, Object>> frank = new Extractor<Map<String, Object>>() {
    		
			@Override
			public String getUrl() {
	        	String urlsports = "http://interface.sina.cn/news/news.photo.wit-list.d.json?page={0}&num={1}";
				showFetching();
				return StrUtil.occupy(urlsports, page, pagesize);
			}
			
			@Override
			protected void parse() {
				item = Amaps.newConcurrentHashMap();
				String temp = source.replace("\\/", "/");
				Mist top = MistUtil.ofJsonText(temp);

				String totalitems = top.findStringBy("total");
				int totalpage = MathUtil.totalPageOf(Integer.parseInt(totalitems), pagesize);
				item.put("totalpage", totalpage);
				
				Object data = top.findBy("data");
				Collections.shuffle((List)data);
				
				item.put("photos", data);
			}
		};
		
		return frank.process().getItem();
	}
	
	public static Album getAlbum(String albumurl) {
		Extractor<Album> frank = new Extractor<Album>() {
    		
			@Override
			public String getUrl() {
				useGBK().showFetching();
				return albumurl;
			}
			
			@Override
			protected void parse() {
				String topic = StrUtil.findFirstMatchedItem("<div class=\"topic-title\">(.+?)</div>", source);
				String when = StrUtil.findFirstMatchedItem("&nbsp;(\\d{4}.+?)</p>", topic);
				Date whenDate = DateUtil.dateOf(when, DateUtil.DATE_TIME);
				String name = StrUtil.findFirstMatchedItem("<h2>(.+?)</h2>", source);

				String r2 = "data-src=\"([^\"]+)\"";
				List<String> links = StrUtil.findAllMatchedItems(r2, source, 1);
				
				String r3 = "<li class=\"desc-text-item\">\\s*<p>(.+?)</p>\\s*</li>";
				List<String> intros = StrUtil.findAllMatchedItems(r3, source, 1);
				XXXUtil.shouldBeEqual(links.size(), intros.size());
				
				List<Object> listObj = Lists.newArrayList();
				for(int k = 0; k < links.size(); k++) {
					Map<String, String> row = Amaps.newLinkHashMap();
					row.put("image_url", links.get(k));
					row.put("intro", intros.get(k));
					listObj.add(row);
				}
				
				item = Album.of(name, links).setTag("sina").setUrl(albumurl).setWhen(when).setWhenDate(whenDate).setListObj(listObj);
			}
		};
		
		return frank.process().getItem();
	}
	
	public static Integer countOfAlbum(String albumurl) {
		Extractor<Integer> frank = new Extractor<Integer>() {
    		
			@Override
			public String getUrl() {
				showFetching();
				return albumurl;
			}
			
			@Override
			protected void parse() {
				String regex = "data-large-src=\"(.+?)\"";
				Matcher ma = createMatcher(regex);
				item = 0;
				while(ma.find()) {
					item++;
				}
			}
		};
		
		return frank.process().getItem();
	}
}
